/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.padrao.presenter;

import br.com.webhomesites.configuracao.model.CidadeModel;
import br.com.webhomesites.configuracao.model.EstadoModel;
import br.com.webhomesites.padrao.contracts.IClienteDAO;
import br.com.webhomesites.padrao.contracts.IClientePresenter;
import br.com.webhomesites.padrao.contracts.IClienteView;
import br.com.webhomesites.padrao.model.ClienteModel;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.AlterarActionListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.CampoKeyListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.CancelarActionListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.InserirActionListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.RemoverActionListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.SairActionListener;
import br.com.webhomesites.padrao.presenter.ClientePresenterActionListeners.SalvarActionListener;
import br.com.webhomesites.padrao.utils.Strategy;

/**
 *
 * @author Webhome
 */
public class ClientePresenter implements IClientePresenter{
    
    private ClienteModel clienteModel;
    private IClienteDAO clienteDAO;
    private IClienteView clienteView;
    private CidadeModel cidadeModel;
    private EstadoModel estadoModel;
    
    public final Strategy INSERT_STRATEGY = new InsertStrategy();  
    public final Strategy UPDATE_STRATEGY = new UpdateStrategy();  
    
    private Strategy operacao = INSERT_STRATEGY;
    
    public ClientePresenter(ClienteModel clienteModel, IClienteView clienteView, IClienteDAO clienteDAO, CidadeModel cidadeModel, EstadoModel estadoModel) {
        this.clienteModel = clienteModel;
        this.clienteView = clienteView;
        this.clienteDAO = clienteDAO;
        this.estadoModel = estadoModel;
        this.cidadeModel = cidadeModel;
    }
    
    @Override
    public void setUpViewListeners() {
        clienteView.setInserirActionListener(new InserirActionListener(this));  
        clienteView.setRemoverActionListener(new RemoverActionListener(this));  
        clienteView.setAlterarActionListener(new AlterarActionListener(this));  
        clienteView.setCancelarActionListener(new CancelarActionListener(this));  
        clienteView.setSalvarActionListener(new SalvarActionListener(this));  
        clienteView.setBtnSairActionListener(new SairActionListener());
        
        clienteView.setMascaraListener(new CampoKeyListener(this));
    }

    @Override
    public void updateModelFromView() {
        clienteModel.setCodigo(clienteView.getCodigo());
        clienteModel.setPessoa(clienteView.getPessoa());
        clienteModel.setNome(clienteView.getNome());
        clienteModel.setRazaoSocial(clienteView.getRazaoSocial());
        clienteModel.setCpf(clienteView.getCpf());
        clienteModel.setRg(clienteView.getRg());
        clienteModel.setCnpj(clienteView.getCnpj());        
        clienteModel.setInscricaoEstadual(clienteView.getInscricaoEstadual()); 
        clienteModel.setEndereco(clienteView.getEndereco());
        clienteModel.setBairro(clienteView.getBairro());
        clienteModel.setCodigoCidade(cidadeModel.getChaveCidade(clienteView.getCidade()));        
        clienteModel.setCodigoEstado(estadoModel.getChaveEstado(clienteView.getEstado()));        
        clienteModel.setTelefone(clienteView.getTelefone());
        clienteModel.setCelular(clienteView.getCelular());
        clienteModel.setEmail(clienteView.getEmail());
        //System.out.println(clienteModel.toString());               
        clienteDAO.inserirCliente(clienteModel);
        System.out.println("******Listagem do Item Insediro = " + "\n");
        clienteModel.toString();
        
    }

    @Override
    public void updateViewFromModel() {
        if (clienteModel != null) {  
            clienteView.setCodigo(clienteModel.getCodigo());  
            clienteView.setRazaoSocial(clienteModel.getRazaoSocial());  
            clienteView.setCnpj(clienteModel.getCnpj());  
            clienteView.setInscricaoEstadual(clienteModel.getInscricaoEstadual()); 
            clienteView.setBairro(clienteModel.getBairro());    
            //clienteView.setCidade(clienteModel.getListaCidades());  
            //clienteView.setEstado(estadoModel.getListaEstados()); 
            clienteView.setTelefone(clienteModel.getTelefone());  
            clienteView.setEmail(clienteModel.getEmail()); 
        }  
        else {  
            clienteView.clearFields();  
        }
    }

    @Override
    public void novoCliente() {
        clienteModel = new ClienteModel(this.clienteDAO);
       
    }

    @Override
    public void inserirCliente() {
        this.updateModelFromView(); 
        clienteView.clearFields();
    }

    @Override
    public void alterarCliente() {
        this.updateModelFromView();
    }

    @Override
    public void removerCliente() {
         this.updateModelFromView();
    }

    @Override
    public void habilitarEdicao(boolean arg) {
        clienteView.enableTxtClienteCodigo(arg);
        clienteView.enableRgClientePessoa(arg);
        clienteView.enableTxtClienteNome(arg);
        clienteView.enableTxtClienteRazaoSocial(arg);
        clienteView.enableTxtClienteCpf(arg);
        clienteView.enableTxtClienteRg(arg);
        clienteView.enableTxtClienteCnpj(arg); 
        clienteView.enableTxtClienteInscricaoEstadual(arg);
        clienteView.enableTxtClienteTelefone(arg);
        clienteView.enableTxtClienteCelular(arg);
        clienteView.enableTxtClienteEmail(arg);
        clienteView.enableTxtClienteEndereco(arg);
        clienteView.enableTxtClienteBairro(arg);
        clienteView.enableComboClienteCidade(arg);
        clienteView.enableComboClienteEstado(arg);
                
        clienteView.enableBtnCancelar(arg);
        clienteView.enableBtnRemover(arg);
        clienteView.enableBtnSalvar(arg);        
    }

    @Override
    public IClienteView getView() {
        return clienteView;
    }
    
    @Override
    public ClienteModel getModel() {
        return clienteModel;
    }
    
    @Override
    public void setView(IClienteView clienteView) {
        this.clienteView = clienteView;
    }

    @Override
    public void setModel(ClienteModel model) {
        this.clienteModel = model;
    }

    @Override
    public void createView() {
        //this.novoCliente();            
        this.setUpViewListeners();  
        //this.habilitarEdicao(false);  
        clienteView.removeAllitensComboClienteCidade();
        clienteView.removeAllitensComboClienteEstado();
        clienteView.setCidade(cidadeModel.getListaCidades());
        clienteView.setEstado(estadoModel.getListaEstados());
        clienteView.packAndShow();
    }    
    
   
    public void setOperacao(Strategy operacao) {
        this.operacao = operacao;
    }

    public Strategy getOperacao() {
        return operacao;
    }
            
     private class InsertStrategy implements Strategy {  
        @Override
        public void execute() {  
            inserirCliente();  
        }  
    }        
      
    private class UpdateStrategy implements Strategy {  
        @Override
        public void execute() {  
            alterarCliente();  
        }  
    }    
}
